/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package serverman;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import javax.swing.*;
/**
 *
 * @author wallis
 */
public class ServerThreads implements Runnable{
    public JTextArea jTextArea1=null;
    BufferedWriter out=null;
    BufferedReader in=null;
    public ClientsList cl=null;
    String sip;
    Integer iport;
    public ServerThreads(ClientsList cl1,String s_ip,Integer i_port,JTextArea jta)
    {
        cl=cl1;
        this.sip=s_ip;
        this.iport=i_port;
        this.jTextArea1=jta;
    }
    @Override
    public void run() {
        ServerSocketThread();
    }
     private void ServerSocketThread() {
        //System.out.println(Thread.currentThread().getName() + " 线程运行开始!");
       jTextArea1.setText(jTextArea1.getText() + Thread.currentThread().getName() + " 线程运行开始!" + "\r\n");
       ServerSocket  server_s=null;
       Socket sk=null;
        try {
          server_s = new ServerSocket(iport);
            //System.out.println("等待客户机"+new java.util.Date(System.currentTimeMillis()));
            jTextArea1.setText(jTextArea1.getText() + "等待客户机" + new java.util.Date(System.currentTimeMillis()) + "\r\n");
            do {
                sk = server_s.accept();
                // System.out.println("已与客户机连接");
                jTextArea1.setText(jTextArea1.getText() + "已与客户机连接" + "\r\n");
                out = new BufferedWriter(new OutputStreamWriter(sk.getOutputStream(),"GBK"));
                in = new BufferedReader(new InputStreamReader(sk.getInputStream()));
                out.write("Server Response OK!\r\n");
                out.flush();
                Thread aThread = new Thread(new CommunicateWithClient(sk,this) );
                aThread.start();
            } while (true);
        } catch (IOException ie) {
            jTextArea1.setText(jTextArea1.getText() + "2:" + ie.toString() + "\r\n");
            //System.out.println(ie.toString());

        } finally {
            try {
                if (server_s != null) {
                    jTextArea1.setText(jTextArea1.getText() + "Closed" + "\r\n");
                    in.close();
                    out.close();
                    sk.close();
                    server_s.close();
                }
            } catch (IOException ie) {
                jTextArea1.setText(jTextArea1.getText() + "3:" + ie.toString() + "\r\n");
            }
        }
    }
     
     private class CommunicateWithClient implements Runnable{
         ServerThreads srvthread;
         Socket sk;
         BufferedWriter out=null;
         BufferedReader in=null;
        public CommunicateWithClient(Socket sk,ServerThreads svs )
        {
            this.srvthread=svs;
            this.sk=sk;
        }
        @Override
        public void run() {
            AddClientSocket();
        }
        private void AddClientSocket()
        {
          String s_msg = null;
        do {
            try {
                s_msg =in.readLine();
                jTextArea1.setText(jTextArea1.getText() + s_msg+"\r\n");
            } catch (IOException ie) {
                jTextArea1.setText(jTextArea1.getText() + "3:" + ie.toString() + "\r\n");
                break;
            }
        } while (true);
        }
    }
     
     
}
